package com.shuoyue.ycgk.ui.mine.vip;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.VipPrice;
import com.shuoyue.ycgk.entity.VipType;
import com.shuoyue.ycgk.ui.pay.PreVipActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.common.BigHintDialog;
import com.shuoyue.ycgk.views.dialog.common.HintDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyVipFragment extends BaseMvpFragment<VipContract.Presenter>
        implements VipContract.View {
    @BindView(R.id.mRecycleView)
    RecyclerView mRecycleView;
    @BindView(R.id.checkbox)
    CheckBox checkbox;


    String title;
    VipType baseType;
    VipAdapter vipAdapter;

    public static MyVipFragment getInstance(VipType type) {
        MyVipFragment fragment = new MyVipFragment();
        fragment.title = type.getName();
        Bundle bundle = new Bundle();
        bundle.putSerializable("questionType", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void initParams(Bundle bundle) {
        baseType = (VipType) bundle.getSerializable("questionType");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_vip;
    }

    @Override
    protected void initView() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycleView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        vipAdapter = new VipAdapter(null);
        vipAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            VipPrice vipPrice = (VipPrice) adapter.getItem(position);
            for (VipPrice item : vipAdapter.getData()) {
                item.setSelect(false);
            }
            vipPrice.setSelect(true);
            adapter.notifyDataSetChanged();
        });
        mRecycleView.setAdapter(vipAdapter);
        registEmptyView(vipAdapter);

    }

    @Override
    protected void initData() {
        mPresenter = new VipContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getVipPrice(baseType.getId());
        if (baseType.isAutoOpen()) {
            BigHintDialog hintDialog = new BigHintDialog(mContext, baseType.getName() + "VIP特权介绍",
                    baseType.getVipFunctionIntroduce(), Gravity.LEFT);
            hintDialog.setCanceledOnTouchOutside(true);
            hintDialog.show();
            checkbox.setChecked(true);
        }
    }

    @OnClick({R.id.rule, R.id.buy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rule:
                BigHintDialog hintDialog = new BigHintDialog(mContext, baseType.getName() + "VIP特权介绍",
                        baseType.getVipFunctionIntroduce(), Gravity.LEFT);
                hintDialog.setCanceledOnTouchOutside(true);
                hintDialog.show();
                checkbox.setChecked(true);
                break;
            case R.id.buy:
                if (checkbox.isChecked()) {
                    VipPrice vip = vipAdapter.getSelect();
                    if (vip != null)
                        PreVipActivity.start(mActivity, vip, vip.getSubjectTypeId(), title);
                    else
                        toast("请选择购买对象");
                } else {
                    XToast.toast(mContext, "请先阅读VIP特权");
                }
                break;
        }
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setBaseType(List<VipType> baseTypes) {

    }

    @Override
    public void setPriceList(List<VipPrice> vipPrices) {
        vipAdapter.resetData(vipPrices);
    }
}
